package com.w7vm.menuweb.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.Root;

import com.w7vm.menuweb.entity.CardapioBuffetWeb;
import com.w7vm.menuweb.entity.ComposicaoCardBuffetWeb;
import com.w7vm.menuweb.entity.RestauranteFilialWeb;
import com.w7vm.menuweb.entity.RestauranteSedeWeb;
import com.w7vm.menuweb.entity.UsuarioWeb;

@Stateless
public class CardapioBuffetDAO extends GenericDAO<CardapioBuffetWeb> {

	
	@PersistenceContext
	private EntityManager em;
	
	public CardapioBuffetDAO() {
		super(CardapioBuffetWeb.class);
	}


	public int saveCardapioBuffet(CardapioBuffetWeb buffetEntity) {
		System.out.println("[IAM-EJB][saveBuffet] :");
		em.persist(buffetEntity);
		em.flush();
		return buffetEntity.getIdCardapioBuffetWeb();
	}
	
	public int alterarStatusCardapioBuffet(CardapioBuffetWeb buffetEntity) {
		System.out.println("[IAM-EJB][ativaCardapioBuffet] :"+ buffetEntity.getIdCardapioBuffetWeb());

		try{
			Query q1 = em.createQuery("UPDATE CardapioBuffetWeb SET diasSemana = ?, dtInicio = ?, dtTermino = ?," +
					" status = ? WHERE idCardapioBuffetWeb = ?");
	
			q1.setParameter(1, buffetEntity.getDiasSemana());
			q1.setParameter(2, buffetEntity.getDtInicio());
			q1.setParameter(3, buffetEntity.getDtTermino());
			q1.setParameter(4, buffetEntity.getStatus());
			q1.setParameter(5, buffetEntity.getIdCardapioBuffetWeb());
			
			int updated = q1.executeUpdate();
			return updated;

		}catch (NoResultException e){
			System.err.println(e.getMessage());
			return 0;
		}
	}


	public CardapioBuffetWeb mergeUpdate(CardapioBuffetWeb buffetEntity) {
		System.out.println("[IAM-EJB][mergeUpdate] :"+ buffetEntity.getIdCardapioBuffetWeb());
		return super.update(buffetEntity);
	}

	public void deleteCardapioBuffet(int id) {
		CardapioBuffetWeb buffetEntity = new CardapioBuffetWeb(); 
		System.out.println("[IAM-EJB][deleteBuffet] :"+ id);
		buffetEntity = em.find(CardapioBuffetWeb.class, id);
		em.remove(buffetEntity);
		em.flush();
	}

	public List<CardapioBuffetWeb> findCardapioBuffetByIDRestauranteSede(int id) {
		System.out.println("[IAM-EJB][findCardapioBuffetByIDRestauranteSede] :"+ id);
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("idRestaurante", id);
		try{
			return super.findByCriterion(CardapioBuffetWeb.FIND_BUFFET_BY_IDRESTAURANTESEDE, parameters);
		}catch (NoResultException e){
			System.err.println(e.getMessage());
			return null;
		}
	}
	
	public CardapioBuffetWeb findCardapioBuffetByID(int entityID) {
		System.out.println("[CARDAPIO-G1][findCardapioBuffetByID] :"+ entityID);
		try{
			Map<String, Object> parameters = new HashMap<String, Object>();
			parameters.put("idBuffet", entityID);   
			return super.findOneResult(CardapioBuffetWeb.FIND_BUFFET_BY_ID, parameters);
		}catch (NoResultException e){
			System.err.println(e.getMessage());
			return null;
		}
	}

	public List<CardapioBuffetWeb> findCardapioBuffetByIDRestauranteFilial(int id) {

		System.out.println("[IAM-EJB][findCardapioBuffetByIDRestauranteFilial] :"+ id);
		
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("idRestaurante", id);
		try{
			return super.findByCriterion(CardapioBuffetWeb.FIND_BUFFET_BY_IDRESTAURANTEFILIAL, parameters);
		}catch (NoResultException e){
			System.err.println(e.getMessage());
			return null;
		}
	}

	
}
